﻿using System;
using System.Collections;
using System.Collections.Generic;
using CH.Reia.Infrastructure.Data;
using CH.Reia.Infrastructure.Entity;
using Newtonsoft.Json;

namespace CH.Reia.Infrastructure.Repository
{
    public static class LazyLoading
    {
        public static Object Builder(IEntity lazyEntity, IDataBase dataBase)
        {
            try
            {
                if (lazyEntity.LazyLoading > 0)
                {
                    foreach (var propertyInfo in lazyEntity.GetType().GetProperties())
                    {
                        if (propertyInfo.PropertyType.FullName.Contains("List"))
                        {
                            var entityType = propertyInfo.PropertyType.GetProperties()[2].PropertyType;
                            var entityCreate = Activator.CreateInstance(entityType);

                            entityCreate.GetType().GetProperty(lazyEntity.GetType().Name).SetValue(entityCreate, lazyEntity, null);
                            entityCreate.GetType().GetProperty("Domain").SetValue(entityCreate, lazyEntity.Domain, null);

                            var listConverted = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(entityType));

                            var listFinded = dataBase.EntityFind(entityCreate);

                            if (listFinded != null)
                            {
                                listFinded.ForEach(q =>
                                                        {
                                                            q.GetType().GetProperty(lazyEntity.GetType().Name).SetValue(q, null, null);

                                                            ((IEntity)q).LazyLoading = lazyEntity.LazyLoading - 1;
                                                            ((IEntity)q).Domain = lazyEntity.Domain;

                                                            var entityLoaded = LazyLoading.Builder(q as IEntity, dataBase);

                                                            listConverted.Add(entityLoaded);
                                                        });
                            }

                            propertyInfo.SetValue(lazyEntity, listConverted, null);
                        }
                        else if (propertyInfo.PropertyType.FullName.Contains("Costruttore"))
                        {
                            var entityObject = lazyEntity.GetType().GetProperty(propertyInfo.Name).GetValue(lazyEntity, null);

                            if (entityObject != null)
                            {
                                ((IEntity)entityObject).LazyLoading = lazyEntity.LazyLoading - 1;
                                ((IEntity)entityObject).Domain = string.IsNullOrEmpty(((IEntity)entityObject).Domain) ? lazyEntity.Domain : ((IEntity)entityObject).Domain;

                                entityObject = dataBase.EntityLoad(entityObject);
                                entityObject = LazyLoading.Builder(entityObject as IEntity, dataBase);
                            }
                            lazyEntity.GetType().GetProperty(propertyInfo.Name).SetValue(lazyEntity, entityObject, null);
                        }
                    }
                }
                return lazyEntity;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}